package com.whj.account.service;

import com.whj.account.captcha.AccountCaptchaException;
import com.whj.account.captcha.AccountCaptchaService;
import com.whj.account.captcha.RandomGenerator;
import com.whj.account.email.AccountEmailException;
import com.whj.account.email.AccountEmailService;
import com.whj.account.persist.Account;
import com.whj.account.persist.AccountPersistException;
import com.whj.account.persist.AccountPersistService;
import org.springframework.beans.factory.annotation.Autowired;

public class AccountServiceImpl implements AccountService {
    public AccountCaptchaService getCaptchaService() {
        return captchaService;
    }

    public void setCaptchaService(AccountCaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    public AccountEmailService getEmailService() {
        return emailService;
    }

    public void setEmailService(AccountEmailService emailService) {
        this.emailService = emailService;
    }

    public AccountPersistService getAccountPersistService() {
        return persistService;
    }

    public void setAccountPersistService(AccountPersistService accountPersistService) {
        this.persistService = accountPersistService;
    }

    private AccountCaptchaService captchaService;
    private AccountEmailService emailService;
    private AccountPersistService persistService;

    @Override
    public String generateCaptchaKey() throws AccountServiceException {
        try {
            return captchaService.generateCaptchaKey();
        }catch (AccountCaptchaException exp) {
            throw new AccountServiceException("产生Captcha Key失败", exp);
        }
    }

    @Override
    public byte[] generateCaptchaImage(String captchaKey) throws AccountServiceException {
        try {
            return captchaService.generateCaptchaImage(captchaKey);
        } catch (AccountCaptchaException exp) {
            throw new AccountServiceException("产生Captcha Image失败", exp);
        }
    }

    @Override
    public void signUp(SignupRequest request) throws AccountServiceException {
        try {
            //验证密码
            if(!request.getPassword().equals(request.getConfirmPassword())) {
                throw new AccountServiceException("密码和确认密码不一致");
            }

            //验证验证码
            captchaService.validateCaptcha(request.getCaptchaKey(), request.getCaptchaValue());

            //保存用户
            Account account = new Account();
            account.setId(request.getUserId());
            account.setName(request.getName());
            account.setPassword(request.getPassword());
            account.setActivated(false);
            account.setEmail(request.getEmail());
            persistService.createAccount(account);

            //发送激活Email
            String url = String.format("%s?id=%s",
                    request.getServiceUrl().endsWith("/") ? request.getServiceUrl() : request.getServiceUrl() + "/",
                    account.getId());

            emailService.SendEmail(request.getEmail(), "用户账户激活", url);
        } catch (AccountCaptchaException exp) {
            throw new AccountServiceException("验证码错误", exp);
        } catch (AccountPersistException exp) {
            throw new AccountServiceException("存储用户失败", exp);
        } catch (AccountEmailException exp) {
            throw new AccountServiceException("发送Email失败", exp);
        }
    }

    @Override
    public void activate(String userId) throws AccountServiceException {
        try{
            Account account = persistService.readAccount(userId);
            account.setActivated(true);
            persistService.updateAccount(account);
        } catch (AccountPersistException exp) {
            throw new AccountServiceException("激活失败", exp);
        }
    }

    @Override
    public void login(String userId, String password) throws AccountServiceException {
        try{
            Account account = persistService.readAccount(userId);
            if(!account.isActivated()) {
                throw new AccountServiceException("请先激活账户");
            }

            if(!account.getPassword().equals(password)) {
                throw new AccountServiceException("密码不正确");
            }
        } catch (AccountPersistException exp) {
            throw new AccountServiceException("登录失败", exp);
        }
    }
}
